<template>
  <el-drawer v-model="addDrawer" size="50%" :show-close="false" @opened="openDrawer" @close="closeDrawer">
    <template #title>
      <h2><svg-icon icon-class="list"></svg-icon><span style="padding-left: 15px">{{ formName }}</span></h2>
      <el-button type="primary" @click="confirmAndSave" :disabled="drawerLoading" v-if="!form[idName]">保存&继续填写</el-button>
      <el-button type="primary" @click="confirmClick" :disabled="drawerLoading" v-if="!(route.path.includes('dataIntegrate') || route.path.includes('achievementSearch'))">保存</el-button>
    </template>
    <template #default>
      <el-form :model="form" ref="addRef" :inline="true" :rules="formRules" label-width="160px" v-loading="drawerLoading">
        <el-row justify="space-between">
          <el-form-item label="省" prop="provinceId" v-if="provinceData.length > 0 || form.provinceId">
            <el-select v-model="form.provinceId" placeholder="请选择省" @change="chooseFormProvince" clearable>
              <el-option v-for="item in provinceData" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="省" v-else>
            <el-input v-model="form.province" clearable disabled></el-input>
          </el-form-item>
          <el-form-item label="市" prop="cityId" v-if="provinceData.length > 0 || form.cityId">
            <el-select v-model="form.cityId" placeholder="请选择市" @change="chooseFormCity" clearable>
              <el-option v-for="item in cityData" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="市" v-else>
            <el-input v-model="form.city" clearable disabled></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="区/县" prop="countyId" v-if="provinceData.length > 0 || form.countyId">
            <el-select v-model="form.countyId" placeholder="请选择区/县" @change="chooseFormCounty" clearable>
              <el-option v-for="item in areaData" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="区/县" v-else>
            <el-input v-model="form.county" clearable disabled></el-input>
          </el-form-item>
          <el-form-item label="文献编号" prop="id">
            <el-input v-model="form.id" placeholder="请输入文献编号" clearable maxlength="17"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="文献名称" prop="literatureName" style="width: 100%">
            <el-input v-model="form.literatureName" placeholder="请输入文献名称" clearable :autosize="{ minRows: 4, maxRows: 8 }"
              type="textarea" show-word-limit maxlength="255" resize="none"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="文献类型" prop="literatureType" style="width: 100%">
            <el-input v-model="form.literatureType" placeholder="请输入文献类型" clearable maxlength="40"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="刊物名称" prop="publication" style="width: 100%">
            <el-input v-model="form.publication" placeholder="请输入刊物名称" clearable :autosize="{ minRows: 4, maxRows: 8 }"
              type="textarea" show-word-limit maxlength="255" resize="none"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="作者" prop="author" style="width: 100%">
            <el-input v-model="form.author" placeholder="请输入作者" clearable :autosize="{ minRows: 4, maxRows: 8 }"
              type="textarea" show-word-limit maxlength="200" resize="none"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="第一作者单位" prop="authorInstitute" style="width: 100%">
            <el-input v-model="form.authorInstitute" placeholder="请输入第一作者单位" clearable
              :autosize="{ minRows: 4, maxRows: 8 }" type="textarea" show-word-limit maxlength="255"
              resize="none"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="出版单位" prop="publisher" style="width: 100%">
            <el-input v-model="form.publisher" placeholder="请输入出版单位" clearable :autosize="{ minRows: 4, maxRows: 8 }"
              type="textarea" show-word-limit maxlength="255" resize="none"></el-input>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="出版日期" prop="publishDate">
            <el-date-picker v-model="form.publishDate" type="date" placeholder="请选择出版日期" value-format="YYYY-MM-DD" />
          </el-form-item>
          <el-form-item label="原始资料编号" prop="rawDataArwid" style="width: 100%">
            <el-space direction="vertical" alignment="flex-start" :size="15" class="upload-space">
              <el-input v-model="form.rawDataArwid" placeholder="请输入原始资料编号" clearable maxlength="20"></el-input>
              <FileUpload v-model="form.rawDataArwFileid" @uploadFile="uploadSuccess('rawDataArwFileid', $event)"
                @delFile="deleteFile('rawDataArwFileid', $event)" />
            </el-space>
          </el-form-item>
        </el-row>
        <el-row justify="space-between">
          <el-form-item label="备注" prop="commentInfo" style="width:100%">
            <el-input v-model="form.commentInfo" placeholder="请输入备注" :autosize="{ minRows: 5, maxRows: 10 }"
              type="textarea" show-word-limit maxlength="2147483647" clearable></el-input>
          </el-form-item>
        </el-row>
      </el-form>
    </template>
  </el-drawer>
</template>
<script setup>
import useOtherData from '@/hooks/useOtherData'
const { route, form, queryData, formName, idName,
  uploadSuccess, deleteFile,
  chooseFormProvince, chooseFormCity, chooseFormCounty,
  addRef, confirmAndSave, addDrawer, confirmClick, checkIdValid, openDrawer, closeDrawer, drawerLoading } = useOtherData()

const { provinceData, cityData, areaData } = toRefs(queryData)

const formRules = reactive({
  provinceId: [{ required: true, message: '请选择省！', trigger: 'change' }],
  cityId: [{ required: true, message: '请选择市！', trigger: 'change' }],
  countyId: [{ required: true, message: '请选择区/县！', trigger: 'change' }],
  id: [
    { required: true, message: '请输入文献编号！', trigger: 'blur' },
    { min: 17, max: 17, message: '长度必须为17位', trigger: 'blur' },
    { validator: checkIdValid, trigger: 'blur' },
  ],
  literatureName: [{ required: true, message: '请输入文献名称！', trigger: 'blur' }],
  literatureType: [{ required: true, message: '请输入文献类型！', trigger: 'blur' }],
  publication: [{ required: true, message: '请输入刊物名称！', trigger: 'blur' }],
  author: [{ required: true, message: '请输入作者！', trigger: 'blur' }],
  authorInstitute: [{ required: true, message: '请输入第一作者单位！', trigger: 'blur' }],
  publisher: [{ required: true, message: '请输入出版单位！', trigger: 'change' }],
  publishDate: [{ required: true, message: '请输入出版日期！', trigger: 'change' }],
  rawDataArwid: [{ required: true, message: '请输入原始资料编号！', trigger: 'blur' }]
})
</script>